package com.woniu.after.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.after.infra.model.Refunds;
import com.woniu.after.infra.mapper.RefundsMapper;
import com.woniu.after.infra.model.RefundsApply;
import com.woniu.after.service.RefundsApplyService;
import com.woniu.after.service.RefundsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.after.service.dto.RefundsDto;
import com.woniu.after.service.form.CrowdRefundsMoneyForm;
import com.woniu.order.client.OrderManageClient;
import com.woniu.order.client.param.AddOrderFlowingParam;
import com.woniu.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 退款表 服务实现类
 * </p>
 *
 * @author jianlang
 * @since 2022-11-26
 */
@Service
public class RefundsServiceImpl extends ServiceImpl<RefundsMapper, Refunds> implements RefundsService {

    @Resource
    private OrderManageClient orderManageClient;

    @Resource
    private RefundsApplyService refundsApplyService;

    @Override
    public RefundsDto crowdRefundsMoney(CrowdRefundsMoneyForm crowdRefundsMoneyForm) {
        RefundsApply refundsApply = new RefundsApply();
        refundsApply.setUserId(crowdRefundsMoneyForm.getUserId());
        refundsApply.setUserNum(crowdRefundsMoneyForm.getUserNum());
        refundsApply.setOrderId(crowdRefundsMoneyForm.getOrderId());
        refundsApply.setOrderNum(crowdRefundsMoneyForm.getOrderNum());
        refundsApply.setBackMoney(crowdRefundsMoneyForm.getBackMoney());
        refundsApply.setProductId(crowdRefundsMoneyForm.getProductId());
        refundsApply.setRefundresonId(crowdRefundsMoneyForm.getRefundresonId());
        refundsApply.setRefundOtherReson(crowdRefundsMoneyForm.getRefundOtherReson());
        refundsApply.setSalesReturn(0);
        refundsApply.setRemarks(crowdRefundsMoneyForm.getRemarks());
        refundsApply.setState("0");
        refundsApply.setApplyTime(DateUtil.parseDateLong());

        refundsApplyService.save(refundsApply);

        Refunds refunds = BeanUtil.copyProperties(crowdRefundsMoneyForm, Refunds.class);
        refunds.setRefundsApplyId(refundsApply.getId());
        save(refunds);

        AddOrderFlowingParam addOrderFlowingParam = new AddOrderFlowingParam();
        addOrderFlowingParam.setOrderId(crowdRefundsMoneyForm.getOrderId());
        addOrderFlowingParam.setFlowingType("众筹退款");
        addOrderFlowingParam.setOrderPrice(crowdRefundsMoneyForm.getBackMoney());
        addOrderFlowingParam.setCreateTime(new Date());

        orderManageClient.addOrderFlowing(addOrderFlowingParam);



        return BeanUtil.copyProperties(refunds, RefundsDto.class);
    }
}
